$(function(){
	var win = window;
	var doc = document;
	var util = win.util = {};
	var server = win.server = {};
	var handler = win.handler = {};
	var render = win.render = {};
	var strategyId=20;
	//富文本编辑器
	var editor;
	util.textEditor=function(){
		KindEditor.ready(function(K) {
	        editor = K.create($("#innerContent"), {
	            resizeType : 1,
	            width  : "100%" ,
	            height :"410px" ,
	            allowPreviewEmoticons : false,
	            allowImageUpload : false,
	            items : [
	                'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
	                'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
	                'insertunorderedlist', '|', 'emoticons', 'image', 'link']
	        });
	    });
	}

	//多个图片的上传
	server.strategyFile=function(){

			var sellerBaseImage = $("#sellerBaseImage");
			sellerBaseImage.empty();
			
	  		//var uploadButton = $("<a id='sellerBaseImage_button' href=''></a>").text("商家图片上传").appendTo(sellerBaseImage) ;

			
			$('.editor').prepend('<h2 class="upload-butt">上传图片</h2>');

			var uploadButton = $('.editor').prepend('<h2 id="sellerBaseImage_button">选择图片</h2>');
			var doUploadButt = $(".upload-butt") ;
			
	  		var sellerBaseImageUploader = new plupload.Uploader({
			    browse_button: "sellerBaseImage" + "_button" ,
			    url:"/jybb-upload/image/upload.do?belongId=0&imageCatalog=f1" 
			}) ;
	  		
	  		sellerBaseImageUploader.init() ;
			
	  		
	  		sellerBaseImageUploader.bind('FilesAdded', function(up, files) {

			    plupload.each(files, function(file) {
			       // var li = $("<li></li>").attr("id",file.id).text(file.name + plupload.formatSize(file.size));
			        /*var delSpan = $("<span>X</span>").appendTo(li).on("click",function(){
			        }) ;*/
			        //$('.editor').prepend(li);
			    });
			    
			});
	  		
	  		sellerBaseImageUploader.bind('FileUploaded', function(up, file,result) {
	  			
	  			var fileId = eval(result.response)[0]
	  			$.ajax({
				 	url :"/jybb-upload/image/getImgById.json?imageId=" +fileId ,
				   	type : "GET" ,
				    dataType: "json" ,
				    success:function(data){
				    	var images = null ;
				    	//alert(1);
				    	console.log(data);
				    	if("200" === data["code"]){
				    		image = data["result"] ;
			    			var html= editor.html();
							editor.html(html+"</br><img src='"+image["imageUrl"]+"' style='display:block;width:500px;height:500px;'/></br>");
				    	}
				    } ,
			        error:function(){
			        	alert("图片列表获取失败!") ;
			        }
		 		}) ;
	  		
	  		});
	  		
	  		sellerBaseImageUploader.bind('Error', function(up,err) {
			    alert("文件上传发生错误，请重试!") ;
			});
	  		
	  		uploadButton.on("click",function(){
				
				doUploadButt.on("click",function(){
					sellerBaseImageUploader.start() ;
					$(this).unbind("click") ;
				}) ;
			
			}) ;
		

	};
	
    //动态添加图文框
    handler.selectCheckbox=function(){
    	$('.selector').delegate('input','click',function(){
    		var _that=$(this);
    		if(!_that.is(':checked')){
    			//取消勾选后处理的事
    			_that.parents('.changeNum').find('.control-label').each(function(){
    				if(_that.val()==$(this).text()){
	    				$(this).parent().remove();
	    			}
    			})
    		}else{
    			//勾选后处理的事
				var controlGroup = $("<div class='control-group'></div>");
				var label = $('<label class="control-label" for="inputEmail"></label>').text(_that.val());
				var controls = $('<div  class="controls" style="float:left;margin-left:10px;"></div>');
				var controlsDiv = $('<div style="float:left;margin-left:10px;position:relative;"></div>');
				if(_that.parents('.changeNum').attr('changeNumFlag') == '1'){
					//适合、特点的渲染
					var falgEvent= $('<a class="btn btn-primary">上传图片</a>');
				}else if(_that.parents('.changeNum').attr('changeNumFlag') == '2'){
					//互动项目的渲染
					var falgEvent = $('<input type="text" placeholder="￥该项目费用，不填表示免费"/>');
				}else if(_that.parents('.changeNum').attr('changeNumFlag') == '3'){
					var falgEvent = $('<a class="btn">上传图片</a>');
				}
				var img = $('<img src="fwaf" style="display:block;width:220px;margin-top:10px;margin-bottom:10px;"  class="img-rounded"/>');
				var textarea = $('<textarea></textarea>');
				var strong = $('<strong style="display:block;font-size:25px;position:absolute;top:50px;left:190px;">X</strong>');
				controlsDiv.append(falgEvent);
				controlsDiv.append(img);
				controlsDiv.append(textarea);
				controlsDiv.append(strong);
				controls.append(controlsDiv);
				controlGroup.append(label);
				controlGroup.append(controls);
    			_that.parents('.selector').after(controlGroup);
    		}
    	})
    }

    //点击添加
    util.add=function(){
    	$('#addBtn').on('click',function(){
    		var data = {
    			//todo
    		};
    		server.add(data).done(function(list){
    			if(list['code']=='200'){
    				alert('添加成功');
    			}
    		});
    	})
    }
    //调用添加接口
    server.add=function(submitData){
    	return $.ajax({
    		url:"",
    		headers:{
    			
    		},
    		type:"json",
    		dataType:"",
    		data:submitData
    	})
    }

    //调页面数据
    server.getDate=function(){
    	return $.ajax({
    		url:"",
    		headers:{
    			
    		},
    		type:"json",
    		dataType:"",
    		data:submitData
    	})
    }

    render.renderDate=function(data){
    	//to do
    }


})

$(function(){
	handler.selectCheckbox();
	//拖拽插件的调用
	$('.sortable').sortable();
	//代用调页面数据函数
	server.getDate().done(function(list){
		render.renderDate(list);
	});
})